from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QLabel,
                            QTextEdit, QComboBox, QTabWidget)
from PyQt6.QtCore import Qt
from qfluentwidgets import (CardWidget, PushButton, LineEdit,
                           FluentIcon as FIF, InfoBar)
from dbutil.db_handler import DatabaseHandler

class UploadWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()
        
    def setup_ui(self):
        layout = QVBoxLayout(self)
        
        # 创建标签页 - 使用 QTabWidget
        self.tab_widget = QTabWidget(self)
        
        # 添加不同类型的题目标签页
        self.tab_widget.addTab(self.create_question_tab("单选题"), "单选题")
        self.tab_widget.addTab(self.create_question_tab("多选题"), "多选题")
        self.tab_widget.addTab(self.create_question_tab("判断题"), "判断题")
        self.tab_widget.addTab(self.create_question_tab("填空题"), "填空题")
        
        # 设置标签页样式
        self.tab_widget.setStyleSheet("""
            QTabWidget::pane {
                border: 1px solid #cccccc;
                border-radius: 4px;
                background: white;
            }
            
            QTabBar::tab {
                padding: 8px 16px;
                margin: 2px;
                background: #f5f5f5;
                border: 1px solid #cccccc;
                border-radius: 4px;
            }
            
            QTabBar::tab:selected {
                background: #1976d2;
                color: white;
            }
            
            QTabBar::tab:hover:!selected {
                background: #e3f2fd;
            }
        """)
        
        layout.addWidget(self.tab_widget)
        
    def create_question_tab(self, q_type):
        """创建题目类型标签页"""
        tab = QWidget()
        layout = QHBoxLayout(tab)
        
        # 左侧：已上传题目列表
        uploaded_card = CardWidget()
        uploaded_layout = QVBoxLayout(uploaded_card)
        uploaded_layout.addWidget(QLabel(f"已上传的{q_type}"))
        # TODO: 添加已上传题目列表
        
        # 右侧：上传新题目
        upload_card = CardWidget()
        upload_layout = QVBoxLayout(upload_card)
        upload_layout.addWidget(QLabel("上传新题目"))
        
        # 题目内容
        content_edit = QTextEdit()
        content_edit.setPlaceholderText("请输入题目内容")
        
        # 选项/答案
        if q_type in ["单选题", "多选题"]:
            options_layout = QVBoxLayout()
            for i in range(4):
                option = LineEdit()
                option.setPlaceholderText(f"选项 {chr(65+i)}")
                options_layout.addWidget(option)
            upload_layout.addLayout(options_layout)
            
            answer = QComboBox()
            answer.addItems([chr(65+i) for i in range(4)])
            upload_layout.addWidget(QLabel("正确答案"))
            upload_layout.addWidget(answer)
            
        elif q_type == "判断题":
            answer = QComboBox()
            answer.addItems(["正确", "错误"])
            upload_layout.addWidget(QLabel("正确答案"))
            upload_layout.addWidget(answer)
            
        else:  # 填空题
            answer = LineEdit()
            answer.setPlaceholderText("请输入答案")
            upload_layout.addWidget(QLabel("答案"))
            upload_layout.addWidget(answer)
        
        # 解析
        explanation = QTextEdit()
        explanation.setPlaceholderText("请输入题目解析")
        upload_layout.addWidget(QLabel("题目解析"))
        upload_layout.addWidget(explanation)
        
        # 上传按钮
        upload_btn = PushButton("上传题目")
        upload_btn.setIcon(FIF.ADD)
        upload_btn.clicked.connect(lambda: self.upload_question(q_type))
        upload_layout.addWidget(upload_btn)
        
        layout.addWidget(uploaded_card)
        layout.addWidget(upload_card)
        
        return tab
        
    def upload_question(self, q_type):
        """上传题目"""
        # TODO: 实现题目上传逻辑
        # 1. 获取输入内容
        # 2. 验证数据
        # 3. 保存到数据库
        # 4. 更新用户积分
        pass 